package cn.getech.data.metadata.controller;

import cn.getech.data.intelligence.common.utils.*;
import cn.getech.data.intelligence.common.validator.ValidatorUtils;
import cn.getech.data.intelligence.common.validator.group.AddGroup;
import cn.getech.data.intelligence.common.validator.group.UpdateGroup;
import cn.getech.data.metadata.entity.CollectionTableBusinessDescriptionEntity;
import cn.getech.data.metadata.entity.meta.CollectionTableField;
import cn.getech.data.metadata.entity.meta.CollectionTableInfo;
import cn.getech.data.metadata.model.dto.AddCollectionRelationDto;
import cn.getech.data.metadata.model.req.collectiontablefield.CollectiionTableFieldListReq;
import cn.getech.data.metadata.model.req.collectiontablefield.CollectiionTableFieldRemarksUpdate;
import cn.getech.data.metadata.model.req.collectiontableinfo.CollectiionTableInfoListReq;
import cn.getech.data.metadata.model.req.collectiontableinfo.CollectionTableInfoChangeNotifyReq;
import cn.getech.data.metadata.model.req.collectiontableinfo.CollectionTableInfoDeleteReq;
import cn.getech.data.metadata.model.req.collectiontableinfo.CollectionTableInfoUpdateReq;
import cn.getech.data.metadata.model.res.CollectionTableFieldRes;
import cn.getech.data.metadata.model.res.CollectionTableInfoListRes;
import cn.getech.data.metadata.model.res.collectiontablestatistics.RelationTable;
import cn.getech.data.metadata.service.CollectionBloodService;
import cn.getech.data.metadata.service.CollectionTableBusinessDescriptionService;
import cn.getech.data.metadata.service.CollectionTableField2Service;
import cn.getech.data.metadata.service.CollectionTableInfoService;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(value="元数据列表",tags={"元数据列表"})
@RestController
@RequestMapping("md/collectiontableinfo")
public class CollectionTableInfoController {
    @Autowired
    private CollectionTableInfoService collectionTableInfoService;
    @Autowired
    private CollectionTableField2Service collectionTableField2Service;
    @Autowired
    private CollectionTableBusinessDescriptionService collectionTableBusinessDescriptionService;

    @ApiOperation("元数据列表")
    @PostMapping("/list")
    public PageRes<PageUtils<CollectionTableInfoListRes>> list(@RequestBody CollectiionTableInfoListReq req){
        ValidatorUtils.validateEntity(req, AddGroup.class);
        PageUtils<CollectionTableInfoListRes> page = collectionTableInfoService.listBy(req);
        return BaseRes.okWithPage(page);
    }

    @ApiOperation("信息")
    @GetMapping("/info/{id}")
    public DataRes<CollectionTableInfo> info(@PathVariable("id") Integer id){
        CollectionTableInfo collectionTableInfo = collectionTableInfoService.getById(id);
        return BaseRes.okWithData(collectionTableInfo);
    }

    @ApiOperation("详细信息")
    @GetMapping("/infoDesc/{id}")
    public R  infoDesc(@PathVariable("id") Integer id){
        R r=collectionTableInfoService.info(id);
        return r;
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @SysLog(value="修改元数据所属",type= OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_LIST_MODULE_CODE)
    public BaseRes update(@RequestBody CollectionTableInfoUpdateReq req){
        ValidatorUtils.validateEntity(req, UpdateGroup.class);
        collectionTableInfoService.update(req);
        return BaseRes.ok();
    }



    @ApiOperation("更改变更通知")
    @PostMapping("/updateChangeNotify")
    @SysLog(value="更改变更通知",type= OperationTypeEnum.OPENCLOSECODE, modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_LIST_MODULE_CODE)
    public BaseRes updateChangeNotify(@RequestBody CollectionTableInfoChangeNotifyReq req){
        ValidatorUtils.validateEntity(req, UpdateGroup.class);
        collectionTableInfoService.update(req);

        return BaseRes.ok();
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    @SysLog(value="元数据删除",type= OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_LIST_MODULE_CODE)
    public BaseRes delete(@RequestBody CollectionTableInfoDeleteReq req){
        collectionTableInfoService.delete(req);
        return BaseRes.ok();
    }

    @ApiOperation("血缘")
    @GetMapping("/bloodRelation/{id}")  ///  /data-metadata/md/collectiontableinfo/bloodRelation/1
    public R   bloodRelation(@PathVariable("id") Integer id,
                             @RequestParam(required = false) Integer type,
                             @RequestParam(required = false,defaultValue = "0") Integer flag
    ) {
        JSONObject res = collectionTableInfoService.getBloodRelationJSON(id,type,flag);
        return R.okWithData(res);
    }

    @ApiOperation("元数据表详情层次结构")
    @PostMapping("/Meta/hierarchy")
    public R  hierarchy(@RequestBody CollectiionTableFieldListReq req){
        ValidatorUtils.validateEntity(req);
        CollectionTableInfo table = collectionTableInfoService.getById(req.getTableId());
        PageUtils<CollectionTableFieldRes> page = collectionTableField2Service.listBy(req);
        R r = R.okWithPage(page);
        r.put("tableType",table.getTableType());
        return r;
    }

    @ApiOperation("元数据表详情修改备注字段")
    @PostMapping("/Meta/hierarchy/remarks/update")
    public R  remarksUpdate(@RequestBody CollectiionTableFieldRemarksUpdate req){
        if(req.getFieldId()==null){
            return R.error("fildId不能为空");
        }
        if(req.getRemarks()==null){
            req.setRemarks("");
        }
        CollectionTableField info= new CollectionTableField();
        info.setId(req.getFieldId());
        info.setRemarks(req.getRemarks());
        info.setUpdateTime(new Date());
        collectionTableField2Service.updateById(info);
        return R.ok();
    }


    @ApiOperation("元数据表详情业务描述编辑")
    @PutMapping("/edit/businessDescription")
    @SysLog(value="编辑元数据表详情业务描述",type= OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_LIST_MODULE_CODE)
    public R businessDescription(@ApiParam(value = "{'table_id':1234,'Business_description_First':'','Business_description_Second':''}") @RequestBody Map<String, Object> params){
//        ValidatorUtils.validateEntity(req);
        collectionTableBusinessDescriptionService.editMetadataDetails(params);
        return R.ok("成功");
    }

    @ApiOperation("元数据表详情业务描述展示")
    @PostMapping("/businessDescription")
    public R Description(@ApiParam(value = "{'table_id':1234}") @RequestBody Map<String, Object> params){
        ValidatorUtils.validateEntity(params);
        CollectionTableBusinessDescriptionEntity DescriptionEntities = collectionTableBusinessDescriptionService.selectDescription(params);
        return R.okWithData(DescriptionEntities);
    }


    @ApiOperation("血缘关系添加")
    @PutMapping("/bloodRelation/add")  ///  /data-metadata/md/collectiontableinfo/bloodRelation/1
    @SysLog(value="血缘关系添加",type= OperationTypeEnum.ADDCODE,
            modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_GATHER_MODULE_CODE)
    public R   bloodRelationAdd(@RequestBody AddCollectionRelationDto addCollectionRelationDto) {
        boolean b = collectionTableInfoService.bloodRelationAdd(addCollectionRelationDto);
        if(!b){
            return R.error("血缘关系已存在");
        }else{
            return R.ok();
        }
    }


    @ApiOperation("移除血缘关系")
    @PutMapping("/removeBloodRelation")  ///  /data-metadata/md/collectiontableinfo/bloodRelation/1
    @SysLog(value="血缘关系删除",type= OperationTypeEnum.DELETECODE,
            modulesType = OperationModulesEnum.DATAMETADATACODE,platformModuleType = ModuleEnum.METADATA_GATHER_MODULE_CODE)
    public R   removeBloodRelation(@RequestBody RelationTable relationTableList) {
        boolean b = collectionTableInfoService.removeBloodRelationPlus(relationTableList);
        return R.ok();
    }


    @ApiOperation("获取新增血缘的数据")
    @GetMapping("/getbloodParameter/{id}")
    public R   getbloodParameterPlus(@PathVariable("id") Integer id) {
        // 传参：21
        JSONObject res = collectionTableInfoService.getbloodParameterPlus(id);
        return R.okWithData(res);
    }

    @ApiOperation("获取新增血缘的数据库")
    @PostMapping("/getbloodParameterDb")
    public R   getbloodParameterDbPlus(@ApiParam(value = "{'sourceId':121,'sourceName':'sourceName'}") @RequestBody Map<String,Object> params ) {
        // 传参：{"sourceId" :1,"sourceName":"bigdata-test-7-test"}
        List<String> getbloodParameterDb = collectionTableInfoService.getBloodParameterDbPlus(params);
        return R.okWithData(getbloodParameterDb);
    }

    @ApiOperation("获取新增血缘的数据表")
    @PostMapping("/getbloodParameterTable")
    public R   getbloodParameterTablePlus(@ApiParam(value = "{'sourceId':1234,'sourceName':'test','currentTableId':12}") @RequestBody Map<String, Object> params  ) {
//      传参  {"sourceId":0,"sourceName":"ods"}
        List<Map<String, Object>> getbloodParameterTable = collectionTableInfoService.getBloodParameterTablePlus(params);
        return R.okWithData(getbloodParameterTable);
    }

    @ApiOperation("获取新增血缘的数据表的字段")
    @GetMapping("/getbloodParameterFiled")
    public R   getbloodParameterFiledPlus(Integer id ) {
        List<CollectionTableFieldRes> getbloodParameterFiled = collectionTableInfoService.getbloodParameterFiledPlus(id);
        return R.okWithData(getbloodParameterFiled);
    }

}
